﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Documents;
using RIDA.Globals;
using RIDA.DragDrop;

namespace RIDA.Toolbox
{
    // Represents a selectable item in the Toolbox/>.
    public class ToolboxItem : ContentControl
    {
        // caches the start point of the drag operation
        public Point? dragStartPoint = null;
         
        static ToolboxItem()
        {
            // set the key to reference the style for this control
            FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(
                typeof(ToolboxItem), new FrameworkPropertyMetadata(typeof(ToolboxItem)));
        }
        public ToolboxItem()
        {

        }

        public ToolboxItem(ToolboxItem other)
        {
            var cloned = ObjectCloner.Clone<ViewModelBase>(other.Content as ViewModelBase);
            var switched = cloned.Switch();
            this.Content = switched;
            this.DataContext = switched;
            this.StartDragPoint = other.StartDragPoint;
        }

        public Point? StartDragPoint
        {
            get { return dragStartPoint; }
            set { dragStartPoint = value; }
        }

        protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
        {
            base.OnPreviewMouseDown(e);
            this.dragStartPoint = new Point?(e.GetPosition(this));
        }

        protected override void OnPreviewMouseUp(MouseButtonEventArgs e)
        {
            base.OnPreviewMouseUp(e);
            this.dragStartPoint = null;

        }
        protected override void OnPreviewMouseMove(MouseEventArgs e)
        {

            if (e.LeftButton != MouseButtonState.Pressed)
            {
                this.dragStartPoint = null;
                return;
            }
            var cloned = new ToolboxItem(this);
            var win = new CrossWindowsLayer(cloned);
            win.StartDragDropProcess();
        }

        
        protected override void OnPreviewDragEnter(DragEventArgs e)
        {
            base.OnPreviewDragEnter(e);
        }
        protected override void OnPreviewDragOver(DragEventArgs e)
        {
            base.OnPreviewDragOver(e);
        }
        protected override void OnPreviewDragLeave(DragEventArgs e)
        {
            base.OnPreviewDragLeave(e);
        }
        protected override void OnQueryContinueDrag(QueryContinueDragEventArgs e)
        {
            base.OnQueryContinueDrag(e);
        }
        protected override void OnPreviewGiveFeedback(GiveFeedbackEventArgs e)
        {
            base.OnPreviewGiveFeedback(e);
        }
    }

    
}
